#include <vector>
#include <iostream>

using namespace std;

class Solution {
public:
    int maxCoins(vector<int>& nums) {
        nums.insert(nums.begin(), 1);
        nums.push_back(1);
        vector<vector<int>> dp(nums.size(), vector<int>(nums.size(), 0));
        for (int len = 1; len <= nums.size() - 2; len++) {
            for (int i = 1; i <= nums.size() - 1 - len; i++) {
                int j = i + len - 1;
                for (int k = i; k <= j; k++) {
                    dp[i][j] = max(dp[i][j], nums[k] * nums[i - 1] * nums[j + 1] + dp[i][k - 1] + dp[k + 1][j]);
                }
            }
        }
        return dp[1][nums.size() - 2];
    }
};

int main() {
    Solution solution;
    vector<int> input = {3, 1, 5, 8};
    int output = solution.maxCoins(input);
    cout << output << endl;
    return 0;
}